﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Space
{
    public class SpaceGarbageCollector
    {
        private SpaceContainer container;

        private readonly Dictionary<uint, uint> autoRemoveList;
        private readonly Dictionary<uint, uint> autoRemoveCounterList;

        public SpaceGarbageCollector(  SpaceContainer container,   ref Dictionary<uint, uint> autoRemoveList,ref Dictionary<uint, uint> autoRemoveCounterList)
        {
            this.container = container;
            this.autoRemoveList = autoRemoveList;
            this.autoRemoveCounterList = autoRemoveCounterList;
        }

        public void CollectItems()
        {
            for (int i=0;i < this.autoRemoveList.Count;i++)
            {
                uint id = this.autoRemoveCounterList.ElementAt(i).Key;
                this.autoRemoveCounterList[id] = this.autoRemoveCounterList[id] + 1;
                
                if (this.autoRemoveList[id] < this.autoRemoveCounterList[id])
                {
                    if (!this.container.IsLocked(id))
                    {
                        try
                        {
                            this.container.Delete(id);
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
    }
}
